package com.qdu.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.qdu.entity.Housetype;
import com.qdu.entity.Rent;
import com.qdu.entity.Users;
import com.qdu.service.HousetypeService;
import com.qdu.service.RentService;
import com.qdu.service.UsersService;

@Controller
@RequestMapping("/rent")
public class RentController {

	@Autowired
	private UsersService userService;
	
	@Autowired
	private HousetypeService housetypeService;
	
	@Autowired
	private RentService rentService;
	
	@GetMapping("/rent/{Uid}/{Hid}")
	public String rent(@PathVariable("Uid")String Uid,@PathVariable("Hid")String Hid,HttpSession session) {
		Users user = (Users)session.getAttribute("sessionuser");
		String Uid2 = user.getUid();
		String Uaccount=user.getUaccount();
		Housetype house= housetypeService.gethousebyuidandhid(Uid, Hid);
		String houseprice=house.getHprice();
	    String Hatt=house.getHatt();
		Integer Uaccount2=Integer.valueOf(Uaccount);
		Integer houseprice2=Integer.valueOf(houseprice);
		Integer Hatt2 = Integer.valueOf(Hatt);
		if(Hatt2==2){
		if(Uaccount2>=houseprice2)
		{
			
			Uaccount2 = Uaccount2-houseprice2;
			String Uaccount3=String.valueOf(Uaccount2);
			user.setUaccount(Uaccount3);
			userService.updateUsers(user);
			Rent rent = new Rent(Uid2,Uid,Hid);
			rentService.addRent(rent);
			house.setHatt("3");
			housetypeService.updateHouse(house);
			session.setAttribute("houselist",housetypeService.getallHouseList());
			return "rentsuccess";
		}
		else {
			return "rentfail";
		}}
		else {
			
			return "houseisnotpass";
		}
		
	}
	@GetMapping("/rentlistbyuid/{rentUid}")
	public String rentlistbyuid(@PathVariable String rentUid,Model model) {
		model.addAttribute("rentlistbyuid",rentService.getRentById(rentUid));
		return "rentlistbyuid";
		
	}
	
}
